using System.Drawing;
using GTA;
using GTA.Native;
using NativeUI;

namespace CustomPeds.Menus;

public class ModelMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.ms_Menu, new Point(0, -107));
    private static bool inited;
    private static string resumeModel;

    public ModelMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            menu.OnItemSelect += OnItemSelect;
            menu.OnMenuClose += OnMenuClose;
            Build();
            inited = true;
        }
    }

    public static void Open()
    {
        if (!inited) return;
        Build();
        menu.Visible = true;
        menu.RefreshIndex();
    }

    private static void Build()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());

        // 记录进入模型菜单前的模型
        resumeModel = (DressingScript.Dressing == Main.Member.P2) ? Main.P2.skin.ModelName : Main.P1.skin.ModelName;

        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Male));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Female));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Player));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_FreeMode));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Cop));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_LSFD_Paramedic));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Army));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_AddonPeds));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Resume));
        menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Find));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));

        for (int i = 0; i <= 7; i++)
        {
            menu.BindMenuToItem(ModelSubMenu.menu, menu.MenuItems[i]);
        }
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        switch (index)
        {
            case 8:
            {
                var list = new System.Collections.Generic.List<string>(System.Enum.GetNames(typeof(PedHash)));
                var rnd = new System.Random(System.Guid.NewGuid().GetHashCode());
                if (DressingScript.Dressing == Main.Member.P2) Main.ReBuildP2(Main.P2.Character, list[rnd.Next(0, list.Count)]); else Main.ReBuildClone(Main.Clone.Character, list[rnd.Next(0, list.Count)]);
                break;
            }
            case 9:
            {
                if (DressingScript.Dressing == Main.Member.P2) Main.ReBuildP2(Main.P2.Character, resumeModel); else Main.ReBuildClone(Main.Clone.Character, resumeModel);
                break;
            }
            case 10:
            {
                string user = Game.GetUserInput(WindowTitle.EnterMessage60, "FreemodeMale01", 30);
                if (!string.IsNullOrEmpty(user))
                {
                    if (DressingScript.Dressing == Main.Member.P2) Main.ReBuildP2(Main.P2.Character, user); else Main.ReBuildClone(Main.Clone.Character, user);
                }
                break;
            }
            case 11:
                DressingScript.Distance();
                break;
            case 12:
                DressingScript.TurnLeft();
                break;
            case 13:
                DressingScript.TurnRight();
                break;
            case 14:
                DressingScript.AngleUp();
                break;
            case 15:
                DressingScript.AngleDown();
                break;
        }
    }

    private static void UpdateModel()
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            Main.P2.skin.ModelName = Origin.GetPedModelName(Main.P2.Character.Model);
            Skin.TryConvertAddonPedName(ref Main.P2.skin.ModelName);
        }
        else
        {
            Main.Clone.skin.ModelName = Origin.GetPedModelName(Main.Clone.Character.Model);
            Skin.TryConvertAddonPedName(ref Main.Clone.skin.ModelName);
            Main.P1.skin.ModelName = Main.Clone.skin.ModelName;
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        // 关闭时不做大规模模型切换与重穿衣，避免退格触发大量游戏状态切换
        try { LogHelper.WriteInfo("ModelMenu closed"); } catch { }
        menu.Visible = false;
        menu.RefreshIndex();
    }
}


